package rise.comunity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.util.Log;

public class UserData {
	private static UserData userData = null;
	private String username, password;
	private Status status;
	private Socket socket;
	//private PrintWriter printWriter;
	//private BufferedReader bufferedReader;

	private UserData(String username, String password, Status status) {
		this.username = username;
		this.password = password;
		this.status = status;
	}

	public void setSocket(Socket socket/*, PrintWriter printWriter,
			BufferedReader bufferedReader*/) {
		synchronized (this) {
			this.socket = socket;
			//this.printWriter = printWriter;
			//this.bufferedReader = bufferedReader;
			this.notifyAll();
		}
	}

	public void closeSocket() throws IOException {
		synchronized (this) {
			//this.printWriter.close();
			//this.bufferedReader.close();
			Log.e("","Closing socket");
			this.socket.close();
			this.socket = null;
		}
	}

	public boolean isSocketClose() {
		synchronized (this) {
			return this.socket.isClosed();
		}
	}

	public boolean existSocket() {
		synchronized (this) {
			return this.socket != null;
		}
	}

	public PrintWriter getPrintWriter() {
		synchronized (this) {
			PrintWriter pw = null;
			try {
				pw = new PrintWriter(socket.getOutputStream());
			} catch (IOException e) {
				Log.e("-","NULL PrinterWriter");
			}
			return pw;
		}
	}

	public BufferedReader getBufferedReader() {
		synchronized (this) {
			if (this.socket == null) {
				try {
					Log.e("-","Waiting");
					this.wait();
					Log.e("-","Stop Waiting");
				} catch (InterruptedException e) {
					Log.e("-", "Wait UserData");
				}
			}
			BufferedReader br = null;
			try {
				br = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
			} catch (IOException e) {
				Log.e("-","NULL BufferedReader");
			}
			return br;
		}
	}

	public String getUsername() {
		synchronized (this) {
			return username;
		}
	}

	public String getPassword() {
		synchronized (this) {
			return password;
		}
	}

	public Status getStatus() {
		synchronized (this) {
			return status;
		}
	}

	public void setStatus(Status status) {
		synchronized (this) {
			this.status = status;
		}
	}

	public synchronized static void createUserData(String username,
			String password, Status status) {
		if(userData == null)
			userData = new UserData(username, password, status);
		// Log.e("-",userData.username+" "+userData.status);
	}

	public static UserData obtainUserData() {
		return userData;
	}

	public synchronized static void clearUserData() {
		if(userData == null)
			return;
		try {
			Log.e("","Clearing UserData");
			if(userData.socket!=null)
			userData.socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			userData = null;
		}
	}

	public synchronized static void updateStatus(Status status) {
		userData.status = status;		
	}
}